Every job a /// tag does - the type of a variable, how big a list or string is, what a script takes and gives back - can also be written as normal code; this page lists both ways side by side.
Some comments in GBML are not just notes to yourself. A line that starts with /// and then an @ word - /// @capacity 16, say - is a tag, and the compiler reads it. It changes what the program is. Most comments are ignored, so a comment that changes the game can be a surprise.
So every tag has a second way to say the same thing, written as normal code with no comment involved. Both ways are fully supported, and neither one is going away. The tags are not deprecated. A project can use one, the other, or a mix, even on the same line of code. Use whichever reads better to you.
This is the whole list. The left column is the way you may already know. The right column is the new way.
| The job | The tag (still works) | Written as code | What kind of thing it is |
|---|---|---|---|
| Say what type a variable is | /// @type {T} above the line | var name: T = value; (or name: T = value; for an instance variable) | a colon and a type after the name |
Say how many a list, array, stack, queue, set or map can hold | /// @capacity N | list<int, N>, array<int, N>, stack<int, N>, queue<int, N>, set<int, N>, map<int, int, N> - the size goes last | a number inside the type |
Say how big a grid is | /// @capacity WxH | grid<int, W, H> | two numbers inside the type |
Say how many characters a string can hold | /// @capacity N | string<N> | a number inside the type |
| Say what type a script's parameter is | /// @arg {T} name | function f(name: T) | a colon and a type after the parameter |
| Say what type a script gives back | /// @returns {T} | function f(): T | a colon and a type after the parentheses |
| Make an array of a set size with every slot starting the same | /// @type {array<T>} + /// @capacity N + a list of N matching values | array_create(N, fill) | a function |
| Make a grid of a set size with every cell starting the same | /// @type {grid<T>} + /// @capacity WxH (the cells start at 0) | grid_create(W, H, fill) | a function |
Make an empty list, stack, queue or set that holds up to N | /// @type {list<T>} + /// @capacity N + [] | list_create(N, example), stack_create(N, example), queue_create(N, example), set_create(N, example) | a function |
Make an empty map that holds up to N pairs | /// @type {map<K,V>} + /// @capacity N + {} | map_create(N, key_example, value_example) | a function |
Seven of them, one for each kind of container. They all do one job: say what the container holds and how big it is, in one line of normal code.
| Function | What it makes | Which tags it can stand in for |
|---|---|---|
array_create(size: int, fill: element) -> array<T> | an array with size slots, every one starting as fill; the array's type follows from fill | /// @type {array<T>} and /// @capacity size, plus writing the starting values out by hand |
grid_create(width: int, height: int, fill: element) -> grid<T> | a grid width across and height down, every cell starting as fill; the grid's type follows from fill | /// @type {grid<T>} and /// @capacity WxH |
list_create(capacity: int, example: element) -> list<T> | an empty list that holds up to capacity | /// @type {list<T>} and /// @capacity capacity |
stack_create(capacity: int, example: element) -> stack<T> | an empty stack that holds up to capacity | /// @type {stack<T>} and /// @capacity capacity |
queue_create(capacity: int, example: element) -> queue<T> | an empty queue that holds up to capacity (a power of two) | /// @type {queue<T>} and /// @capacity capacity |
set_create(capacity: int, example: element) -> set<T> | an empty set that holds up to capacity (a power of two) | /// @type {set<T>} and /// @capacity capacity |
map_create(capacity: int, key_example: element, value_example: element) -> map<K,V> | an empty map that holds up to capacity pairs (a power of two) | /// @type {map<K,V>} and /// @capacity capacity |
The other rows of the first table are not functions. They are a small addition to how you write a type, so they add no function names to remember.
The first two are different from the last five, and the difference is the whole reason there are two names for it: fill and example.
array_create(5, 0) really does put 0 in all five slots. That is fill.list_create(16, 0) has no items and a length of 0. The 0 is only an example of the kind of thing that will go in, so GBML knows it is a list of whole numbers, and is never put in the list. A map takes two, one example of a key and one of a value.An example is any value of the right kind: 0 for whole numbers, 0.0 for decimals, "" for text, true for yes-or-no, { x: 0, y: 0 } for a struct. It is never run and never stored.
An instance variable (declared in the Create event) written both ways:
// the tag way
/// @type {list<int>}
/// @capacity 16
inventory = [];
// the written-out way - exactly the same variable
bag: list<int, 16> = [];
name: string<64> = "";
board: grid<int, 8, 6> = [];
scores = array_create(5, 0);
names = list_create(8, "");And a variable inside an event, plus a script's signature:
var seen: list<int, 8> = [];
var flags = array_create(200, false);
var map_cells = grid_create(8, 6, -1);
var order = queue_create(8, 0);// scripts/heal.gbml - the same script, both ways.
/// @arg {int} amount
/// @arg {list<int>} log
/// @returns {real}
function heal(amount, log) { ... }
function heal(amount: int, log: list<int>): real { ... }A list<int, 16> or array_create(16, 0) needs a number you can see in the code - 16, not a variable like n. This is not a rule GBML invented. It comes from the machine.
The Game Boy Advance has no way to hand out memory while a game runs. A container's size is fixed when the game is built, and stored in the program itself (it is part of the container's type in the C++ the compiler writes). A variable such as n does not have a value until the game is running, which is far too late. So the compiler needs the number right there in your code.
That is exactly what a /// @capacity tag has always needed, too. The only thing that was ever optional was writing it inside a comment. If you give a variable there instead, the compiler says so and explains why:
`array_create` needs its size to be a whole number written right there in the code,
but this is the variable `n`.list<int> works on the caller's own list, whatever size that one has, so function f(a: list<int, 4>) is refused. Give the size where the variable is first declared.list<list<int, 4>> is refused: the type inside a container always gets the standard size.grid<int, 8> is refused, because a board needs to know how wide it is, not just how many squares it has. Write grid<int, 8, 6>./// @capacity 4 tag above a list<int, 9> disagrees with itself, and is refused rather than one of them winning.var line. var a: int = 1, b: int = 2; is refused; put each on its own line._create functions set the size of the variable they are given to, so they must be the whole right-hand side of the line that declares it - not tucked inside another expression.nothing cannot be an example. list_create(8, nothing) does not say what kind of thing goes in, so it is refused. Write var enemies: list<ref<obj_enemy>, 8> = []; instead.All of these work, and you can mix them. Some guidance for choosing:
list_create(16, "") reads as one short, normal line, and the example for a list of structs (list_create(8, { x: 0, y: 0 })) also tells GBML what a struct in it looks like.var inventory: list<string, 16> = []; says the type, the size and that it starts empty without making up an example value. It is the one to use for a list of references to instances (list<ref<obj_enemy>, 16>), because there is no value to give as an example of "an enemy that does not exist yet", and for a string, which has no _create function.All of them are the same size, the same speed and the same list in the finished game.
A list starts empty. So when you write list_create(16, 0), the 0 cannot be something that goes in the list, because nothing goes in. It is only there to answer one question - "a list of what?" - and 0 answers "whole numbers." An array has no such problem: all its slots exist, so its second value is the thing every slot holds, and it names the kind as a side effect.
If you would rather not hand over a value that is never used, write the type instead: var inventory: list<string, 16> = []; says the same thing with nothing made up.